﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using DapperExtensions;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.Crm.CommonRepository;
using ZilLion.Service.WechatService.Domain.WebApiRepository.InterFace;
using AccBindingCardMemberno = ZilLion.Service.Crm.CommonEntity.AccOpr.Web.AccBindingCardMemberno;
using AccBindingCardYwdetail = ZilLion.Service.Crm.CommonEntity.AccOpr.Web.AccBindingCardYwdetail;

namespace ZilLion.Service.WechatService.Domain.WebApiRepository
{
    public class BindOldCardRepository : CrmSqlServerRespository<WebApiEntity.WechatCardModel.AccBindingCardYwdetail>, IBindOldCardRepository
    {
        public BindOldCardRepository(long? accid) : base(accid)
        {
        }

        public BindOldCardRepository(IDbContext context, long? accid) :base(context)
        {
            this.Accid = accid.GetValueOrDefault();
        }

        public AccBindingCardMemberno BindOldCard(AccBindingCardYwdetail info)
        {
               DynamicParameters p = CreateDynamicParameters(new ParamModel(ParamLabel: "accid", Val: Accid));
            List<Action<IDbConnection>> paActions = new List<Action<IDbConnection>>();
            var ac = ExecuteProcAddParam(info.Ywcore, "#ywcore", true);
            paActions.Add(ac);
            AccBindingCardMemberno memberno = new AccBindingCardMemberno();
            var ywInfoparam = ExecuteProcAddParam(memberno, "#memberno", false);
            paActions.Add(ywInfoparam);
            ExecuteProc(paActions, "pr_AccBindingCard_submit_new", p, (con) =>
            {
                memberno = con.GetList<AccBindingCardMemberno>().FirstOrDefault();
            });
            return ReturnResponseResultInfo<AccBindingCardMemberno>(p) ?? memberno?? new AccBindingCardMemberno();
        }
    }
}